package com.cy.module;

import com.cy.ext.plugin.route.ControllerBind;
import com.cy.model.Permission;
import com.jfinal.plugin.activerecord.Page;

@ControllerBind(controllerKey = "/clmp/permission")
public class PermissionController extends WebApiController {
    @Override
    public void index() {
        Permission permission = getModel(Permission.class);
        int page = getParaToInt("pageIndex");
        int pageSize = getParaToInt("limit");
        Page<Permission> permissionPage = Permission.dao.paginate(page, pageSize, Permission.SELECT_ALL, permission);
        renderBuiPage(permissionPage, 200);
    }

    @Override
    public void show() {
    }

   @Override
    public void update() {
        int id = getParaToInt();
        Permission tempPermission = getModel(Permission.class);
        Permission permission = Permission.dao.findById(id);
        permission.cover(tempPermission);

        if (permission.update()) {
            renderSuccess();
        } else {
            renderFail();
        }
    }

    @Override
    public void save() {
        Permission permission = getModel(Permission.class);
        if (permission.save()) {
            renderSuccess();
        } else {
            renderFail();
        }
    }

    @Override
    public void delete() {
        String[] ids = getPara().split("-");
        for (String id : ids) {
            Permission.dao.deleteById(id);
        }
        renderSuccess();
    }
}
